package com.qcby.llmtchstd.controller;

import com.qcby.llmtchstd.common.ResponseResult;
import com.qcby.llmtchstd.entity.Student;
import com.qcby.llmtchstd.service.ClassService;
import com.qcby.llmtchstd.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/student")
@Api(tags = "学生管理")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;


    @RequestMapping(value = "/count",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取数量" )
    public ResponseResult count(){
        return studentService.count();
    }

    @RequestMapping(value = "/page",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("显示学生信息" )
    public ResponseResult page(@RequestBody List<Integer> params){
        Integer page = params.get(0);
        Integer pageSize = params.get(1);
        return studentService.page(page,pageSize);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("添加学生信息" )
    public ResponseResult add(@RequestBody Student student){
        ResponseResult res0 = studentService.snoUp(student);
        if(res0.getCode()!="0000"){
            return res0;
        }
        ResponseResult res = studentService.phoneUp(student);
        if(res.getCode()=="0000"){
            return studentService.add(student);
        }
        return res;
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("查询学生信息" )
    public ResponseResult search(@RequestBody Student student){
        return studentService.search(student);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("修改学生信息" )
    public ResponseResult update(@RequestBody Student student){
        ResponseResult res0 = studentService.snoUp(student);
        if(res0.getCode()!="0000"){
            return res0;
        }
        ResponseResult res = studentService.phoneUp(student);
        if(res.getCode()=="0000"){
            return studentService.update(student);
        }
        return res;


    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("删除学生信息" )
    public ResponseResult delete(@RequestBody Integer id){
        return studentService.delete(id);
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("查询要修改的学生信息" )
    public ResponseResult edit(@RequestBody Integer id){
        return studentService.edit(id);
    }

    @RequestMapping(value = "/serCount",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取搜索后的数量" )
    public ResponseResult serCount(@RequestBody Student student){
        return studentService.serCount(student);
    }

    @RequestMapping(value = "/countById",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("根据教师获取数量" )
    ResponseResult countById(@RequestBody Integer id){
        return studentService.countById(id);
    }

    @RequestMapping(value = "/pageById",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("根据教师显示学生信息" )
    public ResponseResult pageById(@RequestBody Student student){

        return studentService.pageById(student);
    }

    @RequestMapping(value = "/searchById",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("根据教师查询学生信息" )
    public ResponseResult searchById(@RequestBody Student student){
        return studentService.searchById(student);
    }

    @RequestMapping(value = "/serCountById",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("根据教师获取搜索后的数量" )
    public ResponseResult serCountById(@RequestBody Student student){
        return studentService.serCountById(student);
    }

    @RequestMapping(value = "/getStudentDetails", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取学生详细信息（包括班级信息）")
    public ResponseResult getStudentDetails(@RequestBody Integer id) {
        ResponseResult studentResult = studentService.edit(id);
        if (!"0000".equals(studentResult.getCode())) {
            return studentResult;
        }

        Student student = (Student) studentResult.getData();
        ResponseResult classResult = classService.edit(student.getClassId());
        if (!"0000".equals(classResult.getCode())) {
            return classResult;
        }

        // 使用完全限定名避免冲突
        com.qcby.llmtchstd.entity.Class clazz = (com.qcby.llmtchstd.entity.Class) classResult.getData();
        student.setClassName(clazz.getName()); // 设置班级名称
        return ResponseResult.success(student);
    }
}
